package com.example.demo.controller;

import com.example.demo.model.User;
import com.sun.deploy.net.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;


/**
 * @program: demo
 * @author: JCM
 * @create: 2023-02-26 16:25
 * @description:
 **/

@RequestMapping("/test")// 路由注册
//@ResponseBody// 告诉程序返回的是一个数据
//@Controller// 让框架启动的时候加载当前类

@RestController // @RestController = @ResponseBody + @Controller
@Slf4j
public class TestController {
//    @RequestMapping("/hi")
//    @RequestMapping(value = "/hi",method = RequestMethod.POST)
//    @PostMapping("/hi")
    @GetMapping("/hi")
    public String sayHi(String name,Integer age) {
        return "hi,spring boot " + name+" age "+age;
    }

    @GetMapping("/num")
    public String getNum(Integer num) {
        return "getNum:"+num;
    }

    // 接收普通对象
    @GetMapping("/show-user")
    public String showUser(User user) {
        return user.toString();
    }

    // 接收  JSON  对象（和第三方系统通讯时常见的场景）
    @PostMapping("/show-JSON-user")
    public String getJSONUser(@RequestBody User user) {
        return user.toString();
    }
    // 参数重命名
    @GetMapping("/show-time")
//    public String showTime(String t,String t2) {
    public String showTime(@RequestParam("t") String start,
                           @RequestParam("t2") String end) {
        // t:开始时间   t2:结束时间
        return "开始时间："+start+" 结束时间："+end;
    }

    //设置非必传参数
    @GetMapping("/show-gender")
    public String showGender(String name,@RequestParam(required = false) Integer age,
                             String gender) {
        return "name:"+name+" age:"+"gender:"+gender;
    }

    @RequestMapping("/login/{username}/{password}")
    public String login(@PathVariable("username") String username,
                        @PathVariable("password") String password) {
        return "username: "+username+" password: "+password;
    }

    // 上传文件
    @RequestMapping("/upfile")
    public String upfile(@RequestPart("myfile")MultipartFile file) throws IOException {
        String path = "D:\\jcm\\Pictures\\img.png";
        // 保存文件
        file.transferTo(new File(path));
        return path;
    }

    @RequestMapping("/myupfile")
    public String myUpFile(@RequestPart("myfile") MultipartFile file) throws IOException {
        // 根目录
        String path = "D:\\jcm\\Pictures\\";
        // 根目录 + 【唯一的文件名】
        path += UUID.randomUUID().toString().replace("-","");
        // 根目录 + 唯一的明文件 + 【文件的后缀】  ex：。png
        path += file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        file.transferTo(new File(path));
        return path;
    }

    @RequestMapping("/file")
    public String updateFile(@RequestPart("updatefile")MultipartFile file) throws IOException {
        String path = "D:\\jcm\\Pictures\\";
        path += UUID.randomUUID().toString().replace("-","");
        path += file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        file.transferTo(new File(path));
        return path;
    }

    // Spring MVC(Spring Web) 内置了 HttpServletRequest 和 HttpServletResponse
    @GetMapping("/getparam")
    public String getParam(HttpServletRequest req) {
        return req.getParameter("username");
    }

    @RequestMapping("/getck")
    public String getCookie(HttpServletRequest req) {
        Cookie[] cookies = req.getCookies();
        for (Cookie item:cookies) {
            log.error(item.getName()+":"+item.getValue());
        }
        return "get cookie";
    }

    @RequestMapping("/getck2")
    public String getCookie2(@CookieValue("lisi") String val) {
        return "Cookie:"+val;
    }

    @RequestMapping("/getheader")
    public String getHeader(@RequestHeader("user-agent") String userAgent) {
        return userAgent;
    }

    @RequestMapping("/setsess")
    public String setSession(HttpServletRequest req) {
        HttpSession session = req.getSession();
        session.setAttribute("userinfo","userinfo");
        return "set session success";

    }

    @RequestMapping("/getsess")
    public String getSession(HttpServletRequest req) {
        HttpSession session = req.getSession(false);
        if(session != null && session.getAttribute("userinfo") != null) {
            return (String) session.getAttribute("userinfo");
        }else {
            return "暂无 session 信息";
        }
    }

    @RequestMapping("/getsess2")
    public String getSession2(@SessionAttribute(value = "userinfo",required = false) String userinfo) {
        return userinfo;
    }
}
